Writing web applications


In [1]:
!pip3 install Flask


Collecting Flask
  Downloading Flask-0.11.1-py2.py3-none-any.whl (80kB)
    100% |████████████████████████████████| 81kB 3.5MB/s 
Collecting itsdangerous>=0.21 (from Flask)
  Downloading itsdangerous-0.24.tar.gz (46kB)
    100% |████████████████████████████████| 51kB 7.0MB/s 
Requirement already satisfied (use --upgrade to upgrade): Jinja2>=2.4 in /usr/local/lib/python3.5/site-packages (from Flask)
Collecting Werkzeug>=0.7 (from Flask)
  Downloading Werkzeug-0.11.10-py2.py3-none-any.whl (306kB)
    100% |████████████████████████████████| 307kB 2.3MB/s 
Collecting click>=2.0 (from Flask)
  Downloading click-6.6.tar.gz (283kB)
    100% |████████████████████████████████| 286kB 2.3MB/s 
Requirement already satisfied (use --upgrade to upgrade): MarkupSafe in /usr/local/lib/python3.5/site-packages (from Jinja2>=2.4->Flask)
Building wheels for collected packages: itsdangerous, click
  Running setup.py bdist_wheel for itsdangerous ... - \ done
  Stored in directory: /Users/xiaoshuyao/Library/Caches/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a
  Running setup.py bdist_wheel for click ... - \ done
  Stored in directory: /Users/xiaoshuyao/Library/Caches/pip/wheels/b0/6d/8c/cf5ca1146e48bc7914748bfb1dbf3a40a440b8b4f4f0d952dd
Successfully built itsdangerous click
Installing collected packages: itsdangerous, Werkzeug, click, Flask
Successfully installed Flask-0.11.1 Werkzeug-0.11.10 click-6.6 itsdangerous-0.24

In [8]:
from flask import Flask
app = Flask(__name__)

@app.route("/blah")
def hello():
    return "Hello, world!"

@app.route("/hello")
def fun_response_yay():
    return "you went to the path /Hello, congratulations!"

@app.route("/")
def index_resource():
    return "welcomt to example server."

app.run()

In [9]:
localhost = 127.0.0.1


  File "<ipython-input-9-ff173b8f3055>", line 1
    localhost = 127.0.0.1
                      ^
SyntaxError: invalid syntax

In [10]:
''.join(reversed("mammoth"))


Out[10]:
'htommam'

In [11]:
''.join(list(reversed("slipup")))


Out[11]:
'pupils'

In [14]:
from flask import Flask,request
app = Flask(__name__)

@app.route("/reverse")
def reverser():
    word = request.args.get('word',None)
    if word:
        word_in_reverse = ''.join(reversed(word))
        return word_in_reverse
    else:
        return("you did not use the API correctly")

app.run()

In [17]:
x = {'orange':6, 'apples':4, 'durians':2}

In [18]:
if "passionfruit" in x:
    print(x['passionfruit'])

In [19]:
x.get('passionfruit',0)


Out[19]:
0

In [20]:
x.get('apples',0)


Out[20]:
4

a more interesting hello world

request: http://localhost:5000/greeting?to_greet=galaxy
response: Hello, galaxy!

addtionally: choose a random greeting

['hello','hi','howdy','hey','sup']

In [21]:
import random

In [23]:
random.randrange(1, 21)


Out[23]:
16

In [24]:
random.uniform(0,1)


Out[24]:
0.3173983790764362

In [25]:
random.choice(['hello','hi','howdy','hey','sup'])


Out[25]:
'howdy'

In [ ]:
from flask import Flask,request
app = Flask(__name__)

greets = ['hello','hi','howdy','hey','sup']
punc = ['!','!!','?',".","....."]

@app.route("/greeting")
def greet_generator():
    thing = request.args['to_greet']
    greeting = random.choice(greets) + " " + thing + random.choice(punc)
    return greeting
app.run()

In [ ]: